package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.DistributorProductsDto;
import com.qf.commerce.common.vo.DistributorProductsVo;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.DistributorProductsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dealer/products")
public class DistributorProductsController {
    @Resource
    private DistributorProductsService distributorProductsService;

    /**
     * 分页展示经销商品
     *
     * @param page page
     * @param size size
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.common.vo.DistributorProductsVo>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<DistributorProductsVo>> list(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "6") Integer size) {
        return ResponseResult.success(distributorProductsService.listAll(page, size));
    }

    /**
     * 添加经销商品
     *
     * @param dpd dpd
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody DistributorProductsDto dpd) {
        try {
            int num = distributorProductsService.addProducts(dpd);
            return ResponseResult.success(num);
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 修改佣金
     *
     * @param dpd dpd
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/commission")
    public ResponseResult<Integer> updateCommission(@RequestBody DistributorProductsDto dpd) {
        try {
            int num = distributorProductsService.updateProducts(dpd);
            return ResponseResult.success(num);
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @DeleteMapping("/delete")
    public ResponseResult<Integer> deleteProducts(Integer DistributorProductsId) {
        try {
            int num = distributorProductsService.deleteByPrimaryKey(DistributorProductsId);
            return ResponseResult.success(num);
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
}
